//
//  CouponViewController.m
//  YJMiGao
//
//  Created by 张龙 on 16/11/18.
//  Copyright © 2016年 张龙. All rights reserved.
//

#import "CouponViewController.h"
#import "ChangeChoiceView.h"
#import "YSSearchViewControllerViewController.h"

@interface CouponViewController ()<UIScrollViewDelegate>

@property(nonatomic,weak) UIScrollView *scrollView;

@property(nonatomic,weak) ChangeChoiceView *choiceView;

@property(nonatomic,strong)NSArray *titleArray;  //头部Array

@property(nonatomic,strong)NSArray *couponTypeArray;  //类型Array

@property(nonatomic,copy)NSString *VCStr;

@end

@implementation CouponViewController
- (instancetype)initWithTitleArray:(NSArray *)titleArray couponTypeArray:(NSArray *)couponTypeArray VCStr:(NSString *)VCStr{
    if (self = [super init]) {
        self.titleArray = titleArray;
        self.couponTypeArray = couponTypeArray;
        self.VCStr = VCStr;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.title = @"学业加油站";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithImage:[UIImage imageNamed:@"search_icon"] highlighted:[UIImage imageNamed:@"search_icon"] target:self selector:@selector(searchGasStation)];
    [self creatScroll];
    self.view.backgroundColor = [Global colorWithHexValue:0xf1f2f4 alpha:1.0];
}

#pragma mark - 搜索学业加油站
- (void)searchGasStation{
    YSSearchViewControllerViewController *searchViewController = [[YSSearchViewControllerViewController alloc]init];
    searchViewController.searchType = KSearchGasStationType;
    [self.navigationController pushViewController:searchViewController animated:YES];
}
- (void)creatScroll{
    double subjectWidth=0.0;
    for (int i =0; i < self.titleArray.count ; i ++) {
        subjectWidth = subjectWidth + [Global getSizeOfString:self.titleArray[i] maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width + 10;
    }
    ChangeChoiceView *choiceview=[[ChangeChoiceView alloc]initWithFrame:CGRectMake(0, 0, subjectWidth + 10, 42) andtitleArray:self.titleArray];
    [self.view addSubview:choiceview];
    self.choiceView=choiceview;

    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(choiceview.frame), kScreenW, kScreenH-CGRectGetMaxY(choiceview.frame) + 20)];
    scrollView.pagingEnabled=YES;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.delegate=self;
    scrollView.contentSize=CGSizeMake(CGRectGetWidth(scrollView.frame)*self.titleArray.count, 0);
    [self.view addSubview:scrollView];
    self.scrollView=scrollView;

    choiceview.clickbuttonblock=^(NSInteger index){
        CGPoint point=self.scrollView.contentOffset;
        self.scrollView.contentOffset=CGPointMake(index*CGRectGetWidth(self.scrollView.frame), point.y);
    };


    for (int i = 0 ; i < self.titleArray.count; i ++) {
        TableViewPresentViewController *inVC=[[NSClassFromString(self.VCStr) alloc]init];
        inVC.subjectIDStr = self.couponTypeArray[i];
        [inVC willMoveToParentViewController:self];
        CGRect rect=scrollView.bounds;
        rect.origin.x = kScreenW * i;
        inVC.view.frame=rect;
        [self addChildViewController:inVC];
        [inVC didMoveToParentViewController:self];
        [scrollView addSubview:inVC.view];
    }


}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index=(NSInteger)(scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame)+0.5);
    [self.choiceView clickbuttonAtIndex:index];
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
